package com.jianglin.controller;


import com.github.pagehelper.PageInfo;
import com.jianglin.model.PropertyInfo;
import com.jianglin.model.PropertyType;
import com.jianglin.model.RecordVo;
import com.jianglin.model.Records;
import com.jianglin.service.IPropertyInfoService;
import com.jianglin.service.IPropertyTypeService;
import com.jianglin.service.IRecordsService;
import com.jianglin.util.JsonObject;
import com.jianglin.util.R;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;


/**
 *
 *
 * @author liyijia
 */
@CrossOrigin(origins="http://localhost:5500")
@RestController
@RequestMapping("/records")
public class RecordsController {


    @Resource
    private IRecordsService recordsService;

    @Resource
    private IPropertyTypeService propertyTypeService;

    @Resource
    private IPropertyInfoService propertyInfoService;


    @RequestMapping("/queryRecordsAll")
   public JsonObject<RecordVo> queryRecordsAll(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "15") Integer limit,
                                     RecordVo recordVo){
       PageInfo <RecordVo> pageInfo=recordsService.findRecordsAll(page,limit,recordVo);
       return new JsonObject<>(0,"ok",pageInfo.getTotal(),pageInfo.getList());

   }


    @RequestMapping("/add")
    @Transactional(rollbackFor = {RuntimeException.class,Error.class})
    public R add(@RequestBody Records records){
        //根据参数房子id和费用类型id 获取最后一次登记信息
        Integer houId=records.getHouseId();
        Integer typeId=records.getTypeId();

        //获取最后一次记录信息
        Records rec=recordsService.queryByHouIdAndTypeId(houId,typeId);
        if(rec!=null){
            //获取上次表的度数  上次抄表时间
            records.setUpTime(rec.getOnTime());
            records.setNum(rec.getNum2());
        }else{
            records.setUpTime(records.getOnTime());
            records.setNum(0.0);
        }

          //添加记录信息到数据库
          records.setCheckTime(new Date());
          recordsService.add(records);

          //2 添加费用信息

        PropertyInfo info=new PropertyInfo();
        info.setHouseId(houId);
        info.setTypeId(typeId);
        //未缴费
        info.setStatus(0);
        if(rec!=null){
            info.setStartDate(rec.getUpTime());
        }else {
            info.setStartDate(records.getOnTime());
        }
        info.setEndDate(records.getOnTime());

        //根据类型的id查询类型的费用标准
        PropertyType type=propertyTypeService.findById(new Long(typeId));
        double  price=type.getPrice();

        //获取度数
        double money=(records.getNum2()-records.getNum())*price;
        info.setMoney(money);
        info.setRemarks(records.getRemarks());
        //添加记录信息
        int num= propertyInfoService.add(info);

        if(num>0){
            return R.ok();
        }else{
            return R.fail("异常");
        }
      }

      @RequestMapping("/deleteByIds")
      @Transactional(rollbackFor = {RuntimeException.class,Error.class})
      public R deleteByIds(String ids){
           //把字符串转list集合
           String[] list= ids.split(",");
           for(String id : list){
               Long idLong=Long.parseLong(id);
               //根据id获取对应的记录信息获取登记时间以及房子id
               Records records=recordsService.findById(idLong);
               //获取房子id
               Integer houId=records.getHouseId();
               //获取时间
               Date onTime=records.getOnTime();
               //删除登记表记录信息
               recordsService.delete(idLong);
               //物业收费信息表相关信息
               propertyInfoService.deleteInfoByHouIdAndTime(houId ,onTime);
           }
          return R.ok();
      }

}
